<?php
namespace app\admin\controller;

use app\common\model\MeritBox as MeritBoxModel;
use app\common\model\Member as MemberModel;
/**
 * 智能功德箱申领控制器
 * Class Adminuser
 * @package app\index\controller
 */
class Meritbox extends Base
{
    /**
     * [index  申领管理]
     * @return mixed|string
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new MeritBoxModel;
        $mmodel = new MemberModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try {
            if(!empty($param['start']) || !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $count = $model->getCount($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getlist($map, $this->from, $this->size);
            foreach($lists as &$v){
                if(!empty($v['mid'])){
                    $mem = $mmodel->findData($v['mid'],'id','username');
                    $v['username'] = $mem['username'];
                }
            }
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看智能功德箱申领列表', 0, $user);
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [excel  导出列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function excel()
    {
        $param = input('param.');
        $model = new MeritBoxModel;
        $user = $this->user;
        $map = [];
        try{
            if(!empty($param['start']) || !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $data = $model->getAllData($map,'id','id,phone,wenum,comname,city,addr,duty,zongpai,message,create_time');

            vendor("PHPExcel.PHPExcel");
            $excel = new \PHPExcel();
            //Excel表格式,这里简略写了4列
            $letter = array('A','B','C','D','E','F','G','H','I','J');
            //表头数组
            $tableheader = array('ID','手机号','微信号','寺院名','地区','详细地址','职务','宗派','来源','申请时间');
            //填充表头信息
            for($i = 0;$i < count($tableheader);$i++) {
                $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
            }
            $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
            $excel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
            $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
            //填充表格信息
            for ($i = 2;$i <= count($data) + 1;$i++) {
                $j = 0;
                foreach ($data[$i - 2] as $key=>$value) {
                    $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                    $j++;
                }
            }

            //创建Excel输入对象
            $write = new \PHPExcel_Writer_Excel5($excel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");;
            header('Content-Disposition:attachment;filename="功德箱申领表.xls"');
            header("Content-Transfer-Encoding:binary");
            $write->save('php://output');
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('功德箱申领导出excel',1,$user);
        return admin_json(1,'导出成功');
    }
}